package com.house365.core.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.baidu.mapapi.map.MapView;
import com.house365.app.analyse.HouseAnalyse;
import com.house365.app.analyse.data.AnalyseMetaData;
import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.anim.AnimBean;
import com.house365.core.application.BaseApplicationWithMapService;
import com.house365.core.constant.CorePreferences;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.view.LoadingDialog;

/**
 * 
 * @author py
 * 
 */
public abstract class BaseBaiduMapActivity extends Activity {
    protected Activity thisInstance;
    protected BaseApplicationWithMapService mApplication;
    protected AsyncImageLoader mAil;
    protected LoadingDialog tloadingDialog;
    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        thisInstance = this;
        registerReceiver(mLoggedOutReceiver, new IntentFilter(ActionTag.INTENT_ACTION_LOGGED_OUT));
        mApplication = (BaseApplicationWithMapService) getApplication();
        preparedCreate(savedInstanceState);
        initView();
        initData();
    }

    protected abstract void preparedCreate(android.os.Bundle savedInstanceState);

    protected abstract void initView();

    protected abstract void initData();

    public AsyncImageLoader getImageLoader() {
        if (mAil == null) {
            mAil = new AsyncImageLoader(this);
        }
        return mAil;
    }

    public void setImage(final ImageView imageView, String imageUrl, final int resId, int scaleType) {
        CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType, getImageLoader());
    }

    public void invalidate(String imageUrl) {
        getImageLoader().invalidate(imageUrl);
    }

    protected void onDestroy() {
        super.onDestroy();
        dismissLoadingDialog();
        tloadingDialog = null;
        if (mAil != null) {
            mAil.clearCacheImage();
        }
        clean();
        getMapView().onDestroy();
        unregisterReceiver(mLoggedOutReceiver);
    }

    private LoadingDialog getLoadingDialog() {
        if (tloadingDialog == null) {
            tloadingDialog = new LoadingDialog(this, R.style.dialog, R.string.loading);
            tloadingDialog.setCancelable(isCancelDialog());
            if (isCancelDialog()) {
                tloadingDialog.setOnCancelListener(new OnCancelListener() {

                    @Override
                    public void onCancel(DialogInterface dialog) {
                        onExitDialog();
                    }
                });
            }

        }
        return tloadingDialog;
    }

    public void showLoadingDialog() {
        if (getLoadingDialog() != null) {
            getLoadingDialog().show();
        }
    }

    public void showLoadingDialog(int resid) {
        if (getLoadingDialog() != null) {
            getLoadingDialog().setMessage(this.getResources().getString(resid));
            getLoadingDialog().show();
        }
    }

    public void dismissLoadingDialog() {
        if (getLoadingDialog() != null) {
            getLoadingDialog().dismiss();
        }
    }

    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    public void showToast(int resId) {
        Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
    }

    public void onExitDialog() {
        thisInstance.finish();
    }

    public boolean isCancelDialog() {
        return true;
    }

    protected void clean() {

    }

    AnalyseMetaData analyseMetadata;

    protected void onResume() {
        super.onResume();
        getMapView().onResume();
        if (CorePreferences.getInstance(this).getCoreConfig().isAnalyse()) {
            analyseMetadata = HouseAnalyse.onPageResume(this);
        }
    }

    protected void onPause() {
        getMapView().onPause();
        super.onPause();
        if (CorePreferences.getInstance(this).getCoreConfig().isAnalyse()) {
            HouseAnalyse.onPagePause(analyseMetadata);
        }
    }

    public abstract MapView getMapView();

    @Override
    public void finish() {
        super.finish();
        AnimBean animBean = getFinishAnim();
        if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
            overridePendingTransition(animBean.getIn(), animBean.getOut());
        }

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        AnimBean animBean = getStartAnim();
        if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
            overridePendingTransition(animBean.getIn(), animBean.getOut());
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        // TODO Auto-generated method stub
        super.startActivityForResult(intent, requestCode);
        AnimBean animBean = getStartAnim();
        if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
            overridePendingTransition(animBean.getIn(), animBean.getOut());
        }
    }

    public AnimBean getStartAnim() {
        // return new AnimBean(R.anim.slide_in_bottom, R.anim.slide_fix);
        return new AnimBean(R.anim.slide_in_right, R.anim.slide_fix);
    }

    public AnimBean getFinishAnim() {
        // return new AnimBean(R.anim.slide_fix, R.anim.slide_out_bottom);
        return new AnimBean(R.anim.slide_fix, R.anim.slide_out_right);
    }
}
